package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itbuka.goods.domain.GoodsClassify;
import com.itbuka.goods.mapper.GoodsClassifyMapper;
import com.itbuka.goods.service.GoodsClassifyService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.List;

/**
* @author ld
* @description 针对表【goods_classify(分类表)】的数据库操作Service实现
* @createDate 2024-07-27 14:50:49
*/
@Service
public class GoodsClassifyServiceImpl implements GoodsClassifyService {
    @Autowired
    private GoodsClassifyMapper  goodsClassifyMapper;

    @Override
    public List<GoodsClassify> selectAll() {
        LambdaQueryWrapper <GoodsClassify> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsClassify::getIsDelete,0);
        return goodsClassifyMapper.selectList(lqw);
    }


    @Override
    public List<GoodsClassify> selectList(GoodsClassify goodsClassify) {
        LambdaQueryWrapper <GoodsClassify> lqw = this.lqw(goodsClassify);
        return goodsClassifyMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsClassify goodsClassify) {
        return goodsClassifyMapper.insert(goodsClassify);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsClassify goodsClassify = new GoodsClassify();
                goodsClassify.setId(Long.parseLong(id));
                goodsClassify.setIsDelete(1);
                goodsClassifyMapper.updateById(goodsClassify);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(GoodsClassify goodsClassify) {
        return goodsClassifyMapper.updateById(goodsClassify);
    }

    @Override
    public Page<GoodsClassify> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<GoodsClassify>)this.selectAll();
    }

    @Override
    public Page<GoodsClassify> pageList(GoodsClassify goodsClassify, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<GoodsClassify>)this.selectList(goodsClassify);
    }

    @Override
    public Integer status(Long id, Integer status) {
        GoodsClassify goodsClassify = new GoodsClassify();
        goodsClassify.setId(id);
        goodsClassify.setStatus(status);
        return  goodsClassifyMapper.updateById(goodsClassify);
    }

    public LambdaQueryWrapper lqw(GoodsClassify goodsClassify){
        LambdaQueryWrapper <GoodsClassify> lqw = new LambdaQueryWrapper<>();
        if (goodsClassify.getId() != null){
            lqw.eq(GoodsClassify::getId,goodsClassify.getId());
        }
        if (goodsClassify.getClassifyName() != null){
            lqw.eq(GoodsClassify::getClassifyName,goodsClassify.getClassifyName());
        }
        if (goodsClassify.getClassifyLogo() != null){
            lqw.eq(GoodsClassify::getClassifyLogo,goodsClassify.getClassifyLogo());
        }
        if (goodsClassify.getSeq() != null){
            lqw.eq(GoodsClassify::getSeq,goodsClassify.getSeq());
        }
        if (goodsClassify.getStatus() != null){
            lqw.eq(GoodsClassify::getStatus,goodsClassify.getStatus());
        }
        if (goodsClassify.getIsDelete() != null){
            lqw.eq(GoodsClassify::getIsDelete,goodsClassify.getIsDelete());
        }
        if (goodsClassify.getCreateTime() != null){
            lqw.eq(GoodsClassify::getCreateTime,goodsClassify.getCreateTime());
        }
        if (goodsClassify.getUpdateTime() != null){
            lqw.eq(GoodsClassify::getUpdateTime,goodsClassify.getUpdateTime());
        }
        if (goodsClassify.getCommission() != null){
            lqw.eq(GoodsClassify::getCommission,goodsClassify.getCommission());
        }
        if (goodsClassify.getIsuperiorId() != null){
            lqw.eq(GoodsClassify::getIsuperiorId,goodsClassify.getIsuperiorId());
        }
        return lqw;
    }
}




